package com.mfoyou.agent.web.controller.account;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;

import org.mfoyou.agent.common.dao.domain.HuawuEvaluate;
import org.mfoyou.agent.common.dao.domain.HuawuEvaluateTag;
import org.mfoyou.agent.common.dao.domain.HuawuGoodsEvaluate;
import org.mfoyou.agent.common.dao.domain.HuawuGoodsEvaluateTag;
import org.mfoyou.agent.common.dao.domain.HuawuServiceOrder;
import org.mfoyou.agent.common.dao.domain.HuawuUser;
import org.mfoyou.agent.common.dao.mfoyou.domain.MfoyouGoodsOrder;
import org.mfoyou.agent.common.inf.ICommon;
import org.mfoyou.agent.common.inf.IGoodsOrder;
import org.mfoyou.agent.common.inf.IOrder;
import org.mfoyou.agent.utils.common.ArrayListObject;
import org.mfoyou.agent.utils.common.JsonRet;
import org.mfoyou.agent.utils.common.ListObject;
import org.mfoyou.agent.utils.common.Logger;
import org.mfoyou.agent.utils.common.MfoyouMap;
import org.mfoyou.agent.utils.json.Json2Object;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mfoyou.agent.web.intercepter.UrlRight;
import com.mfoyou.agent.web.intercepter.UserRight;
import com.mfoyou.agent.web.service.account.AccountService;
import com.mfoyou.agent.web.service.session.SessionService;

@Path("evaluate")
@Service
public class EvaluateController {

    private static final Logger logger = Logger.getLogger(EvaluateController.class);

    @Autowired
    SessionService sessionService;
    @Autowired
    ICommon iCommon;
    @Autowired
    IOrder iOrder;
    @Autowired
    AccountService accountService;
    @Autowired
    IGoodsOrder iGoodsOrder;

    @GET
    @Path(value = "/getEvaluatetags")
    @Produces("application/json;charset=UTF-8")
    @UrlRight(UserRight.ALLACCESS)
    public JsonRet<?> getEvaluatetags(@Context HttpServletRequest request,
                                      @QueryParam("agentId") Integer agentId, @QueryParam("score") Integer score) {
        List<HuawuEvaluateTag> list = iCommon.getTagList(score);
        return JsonRet.buildSuccRet(list);
    }

    @POST
    @Path(value = "/addEvaluate")
    @Produces("application/json;charset=UTF-8")
    @UrlRight(UserRight.LOGINACCESS)
    public JsonRet<?> addEvaluate(@Context HttpServletRequest request, String json) {
        MfoyouMap mfoyouMap = Json2Object.json2MfoyouMap(json);
        HuawuEvaluate huawuEvaluate = new HuawuEvaluate();
        huawuEvaluate.setAgentId(mfoyouMap.getInteger("agentId"));
        huawuEvaluate.setCustomerId(mfoyouMap.getInteger("userId"));
        huawuEvaluate.setEvaluateContent(mfoyouMap.get("content"));
        huawuEvaluate.setEvaluateStar(mfoyouMap.getInteger("star"));
        huawuEvaluate.setOrderId(mfoyouMap.getInteger("orderId"));
        int ret = iOrder.evaluateOrder(mfoyouMap.getInteger("orderId"));
        if (ret == 0) {
            return JsonRet.buildRet(-55);
        }
        //获取站长id
        Integer stationId = mfoyouMap.getInteger("stationId");
        if (stationId != null) {
            huawuEvaluate.setStationId(stationId);
        }
        if (huawuEvaluate.getStationId() == null) {
            HuawuServiceOrder serviceOrderInfo = iOrder.getServiceOrderInfo(mfoyouMap.getInteger("orderId"));
            huawuEvaluate.setStationId(serviceOrderInfo.getStationId());
        }
        huawuEvaluate.setWorkerId(mfoyouMap.getInteger("workerId"));
        huawuEvaluate.setEvaluateTag(mfoyouMap.get("tag"));
        logger.info(sessionService.currentUserId(request) + "  ==>  " + mfoyouMap.getInteger("userId"));
        int cuu = iCommon.addHuawuEvaluate(huawuEvaluate);
        return JsonRet.buildRet(cuu - 1);
    }

    /**
     * 商品服务订单评论
     *
     * @param request
     * @param evaluateId
     * @return
     */
    @GET
    @Path(value = "/deleteEvaluate")
    @Produces("application/json;charset=UTF-8")
    @UrlRight(UserRight.AGENTACCESS)
    public JsonRet<?> deleteEvaluate(@Context HttpServletRequest request, @QueryParam("evaluateId") Integer evaluateId) {
        Integer agentId = sessionService.currentUserId(request);
        int deleteEvaluate = iCommon.deleteEvaluate(evaluateId, agentId);
        return JsonRet.buildRet(deleteEvaluate - 1);
    }

    /**
     * 删除商品评论
     *
     * @param request
     * @param evaluateId
     * @return
     */
    @GET
    @Path(value = "/deleteGoodsEvaluate")
    @Produces("application/json;charset=UTF-8")
    @UrlRight(UserRight.AGENTACCESS)
    public JsonRet<?> deleteGoodsEvaluate(@Context HttpServletRequest request, @QueryParam("evaluateId") Integer evaluateId) {
        Integer agentId = sessionService.currentUserId(request);
        int deleteEvaluate = iCommon.deleteGoodsEvaluate(evaluateId, agentId);
        return JsonRet.buildRet(deleteEvaluate - 1);
    }

    /**
     * 商品订单评论
     *
     * @param request
     * @param response
     * @param pageId     页数
     * @param pageSize   页大小
     * @param minScore   最小评分
     * @param maxScore   最大评分
     * @param agentId    服务商id
     * @param orderType  订单类型  1位服务订单 2位商品订单
     * @param runerId    跑腿员id
     * @param orderId    订单id
     * @param key        搜索的key
     * @param customerId 用户id
     * @param type       种类
     * @param stationId  站长id
     * @return
     */
    @GET
    @Path("evaluate/list")
    @Produces("application/json;charset=UTF-8")
    @UrlRight(UserRight.LOGINACCESS)
    public JsonRet<?> getOrderEvaluateList(@Context HttpServletRequest request, @Context HttpServletResponse response, @QueryParam("pageId") Integer pageId, @QueryParam("pageSize") Integer pageSize,
                                           @QueryParam("minScore") Integer minScore, @QueryParam("maxScore") Integer maxScore, @QueryParam("agentId") Integer agentId,
                                           @QueryParam("orderType") Integer orderType, @QueryParam("runerId") Integer runerId, @QueryParam("orderId") Integer orderId,
                                           @QueryParam("key") String key, @QueryParam("customId") Integer customerId, @QueryParam("type") Integer type,
                                           @QueryParam("stationId") Integer stationId) {
        String start = request.getParameter("startDate");
        String end = request.getParameter("endDate");
        Date startDate = start == null ? null : new Date(Long.parseLong(start));
        Date endDate = end == null ? null : new Date(Long.parseLong(end));
        if (key != null) {
            if (key.length() == 11) {
                HuawuUser runer = accountService.userInfoByPhoneV1(key, -2);
                if (runer == null) return JsonRet.buildRet(-52);
                runerId = runer.getUserId();
            } else {
                HuawuUser runer = accountService.userInfoByName(key, -2);
                if (runer == null) return JsonRet.buildRet(-52);
                runerId = runer.getUserId();
            }
        }
        HuawuEvaluate huawuEvaluate = new HuawuEvaluate();
        huawuEvaluate.setAgentId(agentId);
        huawuEvaluate.setCustomerId(sessionService.currentUserId(request));
        huawuEvaluate.setOrderType(orderType);
        huawuEvaluate.setWorkerId(runerId);
        huawuEvaluate.setCustomerId(customerId);
        huawuEvaluate.setOrderId(orderId);
        huawuEvaluate.setStationId(stationId);
        //后期传个type进去  根据type加字段  后来者优化  当前没时间
        ArrayListObject<HuawuEvaluate> listObject = iOrder.getEvaluate(huawuEvaluate, pageId, pageSize, startDate, endDate, minScore, maxScore);
        if (listObject != null) {
            return JsonRet.buildSuccRet(ListObject.BulidListObject(listObject));
        }
        return JsonRet.buildRet(-97);
    }

    @GET
    @Path(value = "/getGoodsEvaluatetags")
    @Produces("application/json;charset=UTF-8")
    @UrlRight(UserRight.ALLACCESS)
    public JsonRet<?> getGoodsEvaluatetags(@Context HttpServletRequest request,
                                           @QueryParam("agentId") Integer agentId, @QueryParam("score") Integer score) {
        List<HuawuGoodsEvaluateTag> list = iCommon.getgoodsTagList(score);
        return JsonRet.buildSuccRet(list);
    }

    /**
     * 添加评论
     *
     * @param request
     * @param json
     * @return
     */
    @POST
    @Path(value = "/addGoodsEvaluate")
    @Produces("application/json;charset=UTF-8")
    @UrlRight(UserRight.LOGINACCESS)
    public JsonRet<?> addGoodsEvaluate(@Context HttpServletRequest request, String json) {
        MfoyouMap mfoyouMap = Json2Object.json2MfoyouMap(json);
        Integer orderid = mfoyouMap.getInteger("orderId");
        if (mfoyouMap.getInteger("orderId") == null) {
            return JsonRet.buildRet(-96);
        }
        HuawuGoodsEvaluate huawuEvaluate = new HuawuGoodsEvaluate();
        huawuEvaluate.setEvaluateContent(mfoyouMap.get("content"));
        huawuEvaluate.setEvaluateStar(mfoyouMap.getInteger("star"));
        huawuEvaluate.setOrderId(orderid);
        int ret = iOrder.evaluateGoodsOrder(mfoyouMap.getInteger("orderId"));
        if (ret == 0) {
            return JsonRet.buildRet(-55);
        }
        MfoyouGoodsOrder goodsOrder = iGoodsOrder.getGoodsOrderByOrderid(orderid);
        huawuEvaluate.setStationId(goodsOrder.getStationId());
        huawuEvaluate.setAgentId(goodsOrder.getAreaAgentId());
        huawuEvaluate.setWorkerId(goodsOrder.getStoreId());
        huawuEvaluate.setCustomerId(goodsOrder.getUserId());
        huawuEvaluate.setEvaluateTag(mfoyouMap.get("tag"));
        logger.info(sessionService.currentUserId(request) + "  ==>  " + goodsOrder.getUserId());
        int cuu = iCommon.addHuawuGoodsEvaluate(huawuEvaluate);
        return JsonRet.buildRet(cuu - 1);
    }

    @GET
    @Path("goodsevaluate/list")
    @Produces("application/json;charset=UTF-8")
    @UrlRight(UserRight.ALLACCESS)
    public JsonRet<?> getOrderGoodsEvaluateList(@Context HttpServletRequest request, @Context HttpServletResponse response, @QueryParam("pageId") Integer pageId, @QueryParam("pageSize") Integer pageSize,
                                                @QueryParam("minScore") Integer minScore, @QueryParam("maxScore") Integer maxScore, @QueryParam("agentId") Integer agentId,
                                                @QueryParam("orderType") Integer orderType, @QueryParam("storeId") Integer storeId, @QueryParam("orderId") Integer orderId,
                                                @QueryParam("key") String key, @QueryParam("customId") Integer customerId, @QueryParam("type") Integer type,
                                                @QueryParam("stationId") Integer stationId) {
        String start = request.getParameter("startDate");
        String end = request.getParameter("endDate");
        Date startDate = start == null ? null : new Date(Long.parseLong(start));
        Date endDate = end == null ? null : new Date(Long.parseLong(end));
        if (key != null) {
            if (key.length() == 11) {
                HuawuUser runer = accountService.userInfoByPhoneV1(key, -2);
                if (runer == null) return JsonRet.buildRet(-52);
                storeId = runer.getUserId();
            } else {
                storeId = accountService.storeInfoByName(key, agentId);
                if (storeId == null) return JsonRet.buildRet(-52);
            }
        }
        HuawuGoodsEvaluate huawuEvaluate = new HuawuGoodsEvaluate();
        huawuEvaluate.setAgentId(agentId);
        huawuEvaluate.setOrderType(orderType);
        huawuEvaluate.setWorkerId(storeId);
        huawuEvaluate.setCustomerId(customerId);
        huawuEvaluate.setOrderId(orderId);
        huawuEvaluate.setStationId(stationId);
        //后期传个type进去  根据type加字段  后来者优化  当前没时间
        ArrayListObject<HuawuGoodsEvaluate> listObject = iOrder.getGoodsEvaluate(huawuEvaluate, pageId, pageSize, startDate, endDate, minScore, maxScore);
        if (listObject != null) {
            return JsonRet.buildSuccRet(ListObject.BulidListObject(listObject));
        }
        return JsonRet.buildRet(-97);
    }

}
